package com.petiot.ui.fragment;

import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.json.JSONException;
import org.json.JSONObject;

import com.petiot.rabbitmq.R;
import com.petiot.rabbitmq.WavIoTClient;
import com.petiot.rabbitmq.WavIoTDevice;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.petiot.rabbitmq.PushService.PushMsgCallback;
import com.petiot.ui.fragment.BaseFragment;


/**
 * �հ�ҳ��
 */
public class Home2Fra extends BaseFragment implements OnClickListener {

    public static String FRAGMENT_TAG = Home2Fra.class.getSimpleName();
    private static final int ThreadPoolSize = 1;
	private ScheduledExecutorService executorService;
	
	String group_id = "A0000";
	String myclient_uid = "ZHUWEI_WAVIOT_VDEVICE_20151111_1";
	
	public WavIoTClient waviotclient = new WavIoTClient(group_id, myclient_uid);
	private PushMsgCallback callBack;
	public WavIoTDevice waviotdevice = new WavIoTDevice(group_id, myclient_uid, "1001", "111", waviotclient.getClientQueueName(), callBack);
	private static Handler handler;
	
	public BarChart barChart;
    public ArrayList<BarEntry> entries=new ArrayList<BarEntry>();
    public BarDataSet dataset;
    public ArrayList<String> labels=new ArrayList<String>();
    //@Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.bar_chart, container, false);
        barChart= (BarChart) view.findViewById(R.id.bar_chart);
        handler = new Handler(){
			public void handleMessage(Message msg){
			     
			//String message=(String)msg.obj;//obj��һ����String�࣬�����Ǳ���࣬���û������Ӧ��
			  //����message�е���Ϣ�����̵߳�UI���иĶ�
			  //����
			String message = msg.getData().getString("msg");
			Log.v(TAG, "���̻߳�õ����� = " + message);
			initEntriesData(message);
			initLableData();
	        show();
			}
		};
		waviotdevice.doFeeding("getSqlite", waviotclient.getChannel(), myclient_uid);
			
        //initEntriesData();
        //initLableData();
        //show();
        return view;
    }
    public void initEntriesData(String message){
    	String humiture1 = null;
    	String humiture2 = null;
    	String humiture3 = null;
    	String humiture4 = null;
    	String humiture5 = null;
    	String humiture6 = null;
    	Log.e(TAG, "��Ҫ������������ = " + message);
    	try {
			JSONObject jsonMsg = new JSONObject(message);
			humiture1 = jsonMsg.getJSONObject("sqldata1").getString("humiture");
			humiture2 = jsonMsg.getJSONObject("sqldata2").getString("humiture");
			humiture3 = jsonMsg.getJSONObject("sqldata3").getString("humiture");
			humiture4 = jsonMsg.getJSONObject("sqldata4").getString("humiture");
			humiture5 = jsonMsg.getJSONObject("sqldata5").getString("humiture");
			humiture6 = jsonMsg.getJSONObject("sqldata6").getString("humiture");
			Log.i(TAG, "sqldata1��ʪ�������� = " + humiture1);
			Log.i(TAG, "sqldata2��ʪ�������� = " + humiture2);
			Log.i(TAG, "sqldata3��ʪ�������� = " + humiture3);
			Log.i(TAG, "sqldata4��ʪ�������� = " + humiture4);
			Log.i(TAG, "sqldata5��ʪ�������� = " + humiture5);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        entries.add(new BarEntry(Float.parseFloat(humiture1), 0));
        entries.add(new BarEntry(Float.parseFloat(humiture2), 1));
        entries.add(new BarEntry(Float.parseFloat(humiture3), 2));
        entries.add(new BarEntry(Float.parseFloat(humiture4), 3));
        entries.add(new BarEntry(Float.parseFloat(humiture5), 4));
        entries.add(new BarEntry(Float.parseFloat(humiture6), 5));
    }
    public void initLableData(){
        labels.add("һ��");
        labels.add("����");
        labels.add("����");
        labels.add("����");
        labels.add("����");
        labels.add("����");
    }
    public void show(){
        dataset= new BarDataSet(entries,"# of Calls");
        dataset.setColors(ColorTemplate.COLORFUL_COLORS);
        BarData data=new BarData(labels,dataset);
        LimitLine line=new LimitLine(10f);
        barChart.setData(data);
//        chart.animateXY(5000,5000);
//        chart.animateX(5000);
        barChart.animateY(3000);
        barChart.setDescription("Humiture Chart");
    }

	public void drawTable(JSONObject content) {
		// TODO Auto-generated method stub
		Log.e(FRAGMENT_TAG, "content = " + content);
		Message msg = handler.obtainMessage();
		Bundle bundle = new Bundle();

		bundle.putString("msg", content.toString());
		msg.setData(bundle);
		handler.sendMessage(msg);

		
	}
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		
	}

}
